package cn.jdy.plugins.service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.GuseBook;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * 企业留言服务类
 * @author wuzhiliang
 *
 */
@Service
public class GuseBookService extends BaseDao<GuseBook> {

	public int getCount(Integer companyId) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  dc.add(Restrictions.eq("companyId", companyId));
	  dc.add(Restrictions.eq("status", "1"));
		return super.count(dc).intValue();
	}

	public List<GuseBook> getList(Integer companyId, int pageNo, int pageSize) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  dc.add(Restrictions.eq("companyId", companyId));
	  dc.add(Restrictions.eq("status", "1"));
	  dc.addOrder(Order.desc("gusetime"));
		List<GuseBook> list = super.find(dc,(pageNo - 1) * pageSize,pageSize);
		return list;
	}

	/**
	 * 查询留言记录数
	 * @param bo
	 * @return
	 */
	public int getCount(GuseBook bo){
		try {
		  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
			if(!StringUtil.isNullOrEmpty(bo.getCompanyId()))
			  dc.add(Restrictions.eq("companyId", bo.getCompanyId()));
			if(!StringUtil.isNullOrEmpty(bo.getUsername()))
			  dc.add(Restrictions.eq("username", bo.getUsername()));
			return super.count(dc).intValue();
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询留言记录数失败！", e);
		}
		return 0;
	}
	
	/**
	 * 查询留言记录
	 * @param bo
	 * @return
	 */
	public List<GuseBook> query(GuseBook bo) {
		List<GuseBook> list = null;
		try {
		  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
			if(!StringUtil.isNullOrEmpty(bo.getCompanyId()))
			  dc.add(Restrictions.eq("companyId", bo.getCompanyId()));

			if(!StringUtil.isNullOrEmpty(bo.getUsername()))
			  dc.add(Restrictions.eq("username", bo.getUsername()));

			dc.addOrder(Order.desc("gusetime"));
			list = super.find(dc,bo.getiDisplayStart(),bo.getiDisplayLength());
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询留言记录失败！", e);
		}
		return list;
	}
	
	
}
